package com.aigou.admin.dao.impl;

import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.Receiving_addressDao;
import com.aigou.admin.entity.Receiving_address;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: xsc_hunter
 * @Date: 2024/12/3) 下午5:10
 * @Version: 1.0
 */

@Repository
public class Receiving_addressDaoImpl implements Receiving_addressDao {
    /**
     * 查询收货地址总数
     * @return 查询数据行数
     */
    @Override
    public Integer selectCount() {
        return JDBCUtils.executeCount("select count(*) from s_receiving_address");
    }
    @Override
    public List<Receiving_address> selectReceivingAddressByLimit(int start, int rows) {
        return JDBCUtils.executeQuery(Receiving_address.class,"select * from s_receiving_address limit ?,?",start,rows);
    }

    @Override
    public List<Receiving_address> selectAll() {
        return JDBCUtils.executeQuery(Receiving_address.class, "select * from s_receiving_address");
    }

    @Override
    public Receiving_address selectById(String id) {
        List<Receiving_address> Receiving_address = JDBCUtils.executeQuery(Receiving_address.class, "select * from s_receiving_address where id = ? ",  id);
        if (Receiving_address.isEmpty()) {
            return null;
        }
        return Receiving_address.get(0);
    }

    @Override
    public Boolean insert(Receiving_address receiving_address) {
        int rows = JDBCUtils.executeUpdate("insert into s_receiving_address(id,receiving_address,receiving_person,mobile_phone,user_id,is_default) values(?,?,?,?,?,?)",
                receiving_address.getId(), receiving_address.getReceiving_address(), receiving_address.getReceiving_person(), receiving_address.getMobile_phone(),receiving_address.getUser_id(),receiving_address.getIs_default());
        return rows > 0;
    }

    @Override
    public Boolean update(Receiving_address receiving_address) {
        int rows = JDBCUtils.executeUpdate("update s_receiving_address set receiving_address=?,receiving_person=?,mobile_phone=?,user_id=?,is_default=? where id = ?",
                receiving_address.getId(), receiving_address.getReceiving_address(), receiving_address.getReceiving_person(), receiving_address.getMobile_phone(),receiving_address.getUser_id(),receiving_address.getIs_default());
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_receiving_address where id = ?", id);
        return rows >0;
    }
}
